package com.bite.f.service.question.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.bite.common.core.domain.TableDataInfo;
import com.bite.f.domain.question.Question;
import com.bite.f.domain.question.dto.QuestionQueryDTO;
import com.bite.f.domain.question.es.QuestionES;
import com.bite.f.domain.question.vo.QuestionDetailVO;
import com.bite.f.domain.question.vo.QuestionVO;
import com.bite.f.elasticsearch.QuestionRepository;
import com.bite.f.mapper.question.QuestionMapper;
import com.bite.f.service.question.IQuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class QuestionServiceImpl implements IQuestionService {
    @Autowired
    private QuestionRepository questionRepository;
    @Autowired
    private QuestionMapper questionMapper;
    @Override
    public TableDataInfo list(QuestionQueryDTO questionQueryDTO) {
        //先查es在查mysql
        //查询es中的数据
        Long count = questionRepository.count();
        if(count <= 0){
            //从数据库中查,并刷新
            refreshQuestion();
        }
        //在从es中查询
        //第二个参数实体类中的字段名
        Sort sort = Sort.by(Sort.Direction.DESC,"creteTime");
        Pageable pageable = PageRequest.of(questionQueryDTO.getPageNum(),questionQueryDTO.getPageSize(),sort);

        Integer difficulty = questionQueryDTO.getDifficulty();
        String keyword = questionQueryDTO.getKeyword();
        Page<QuestionES> questionESPage;
        if (difficulty == null && StrUtil.isEmpty(keyword)) {
            questionESPage = questionRepository.findAll(pageable);
        } else if (StrUtil.isEmpty(keyword)) {
            questionESPage = questionRepository.findQuestionByDifficulty(difficulty, pageable);
        } else if (difficulty == null) {
            questionESPage = questionRepository.findByTitleOrContent(keyword, keyword, pageable);
        } else {
            questionESPage = questionRepository.findByTitleOrContentAndDifficulty(keyword, keyword, difficulty, pageable);
        }

        long total = questionESPage.getTotalElements();
        if (total <= 0) {
            return TableDataInfo.empty();
        }
        return  TableDataInfo.success(BeanUtil.copyToList(questionESPage.getContent(), QuestionVO.class),total);
    }

    @Override
    public QuestionDetailVO detail(Long questionId) {
        QuestionES questionES = questionRepository.findById(questionId).orElse(null);
        QuestionDetailVO questionDetailVO = new QuestionDetailVO();
        if(questionES != null){
            BeanUtil.copyProperties(questionES,questionDetailVO);
            return questionDetailVO;
        }
        //从数据库中查询
        Question question = questionMapper.selectById(questionId);
        if(question == null){
            return null;
        }
        //刷新es
        refreshQuestion();
        //将数据库查询到的结果返回
        BeanUtil.copyProperties(question,QuestionDetailVO.class);
        return questionDetailVO;
    }

    @Override
    public String preQuestion(Long questionId) {
        //先将redis题目列表拿出
        return null;
    }

    @Override
    public String nextQuestion(Long questionId) {
        return null;
    }


    private void refreshQuestion() {
        //查询所有数据
        List<Question> questions = questionMapper.selectList(new LambdaQueryWrapper<Question>());
        if (CollectionUtil.isEmpty(questions)) {
            return;
        }
        //否则同步给es,传递的应该是QuestionES
        List<QuestionES> questionES = BeanUtil.copyToList(questions, QuestionES.class);
        questionRepository.saveAll(questionES);
    }
}

































